package au.org.arcs.dss.client.tds.utils;

import java.io.*;
import java.util.Date;

import au.org.arcs.dss.provider.ConfigManager;

/**
 *
 * @author ming
 */
public class SystemUtils {
  private static String NC_FILE_EXTENSION_NAME = ".nc";
  private static String GC_FILE_EXTENSION_NAME = ".gc";
  private static String PREFIX_TEMP = "temp";

  public static String getDataSetPath(String path, String key) {
    //dataSetPath = dataSetPath.split(FILE_EXTENSION_NAME)[0] + FILE_EXTENSION_NAME;
    String dataSetPath = "";

    if (path == null || path.isEmpty()) {
      return "";
    } else {
      dataSetPath = path.trim().substring(key.length());
      
      int index = 0;
      for (int i = dataSetPath.length()-1; i >= 0; i--) {
        if (dataSetPath.charAt(i) == '.') {
          index = i;
          break;
        }
      }

      dataSetPath = dataSetPath.substring(0, index);
    }

    return dataSetPath;
  }

  /*public static void writeFile(byte[] data, Date lastModified, FileOutputStream output) {
    try {
      //output = new FileOutputStream(file);
      output.write(data);
    } catch (Exception ex) {
      ConfigManager.logger.error(ConfigManager.key + ": Error creating temp file for File Server.", ex);
    } finally {
      if (output != null) {
        try {
          output.close();
        } catch (Exception ex) {}
      }
    }
  }*/

  public static String[] getPathKey(String path) {
    String[] twoParts = path.split(":");

    if (twoParts == null || twoParts.length != 2) {
      ConfigManager.logger.error(ConfigManager.key + ": Error finding path key.");
      return null;
    }

    // Key is in the first part.
    // Path is in the second part.
    return twoParts;
  }
}